<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="./vue.js"></script>
</head>

<body>
  <div id="vm">
    <cpn></cpn>
    <cpn></cpn>
    <cpn></cpn>
    <!-- 组件实例 -->
  </div>
  <script type="text/x-template" id="cpn">
    <div>
      <h1>当前计数：{{counter}}</h1>
      <button v-on:click="add">+</button>
      <button v-on:click="sub">-</button>
    </div>
  </script>
  <script>
    Vue.component('cpn', { //传入对象
      template: '#cpn',
      data() {
        return {
          counter: 0
        }
      },
      methods: {
        add: function () {
          //在methods中不要这么写，这里会去找全局的变量，这里会找不到报错
          //counter++; 
          this.counter++; //使用this语法来获取当前对象
          console.log('add被执行');

        },
        sub: function () {
          this.counter--;
          console.log('sub被执行');

        }
      }
    }) //全局组件注册
    var vm = new Vue({
      el: '#vm',
      data: {
        name: 'Taec'
      }
    })
  </script>
</body>

</html>